package com.tyj.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tyj.movie.entity.Movie;
import com.tyj.movie.entity.MovieArea;
import com.tyj.movie.entity.MovieType;
import com.tyj.movie.service.MovieAreaService;
import com.tyj.movie.service.MovieService;
import com.tyj.movie.service.MovieTypeService;
import com.tyj.movie.util.R;
import com.tyj.movie.vo.MovieDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    MovieService movieService;

    @Autowired
    MovieTypeService movieTypeService;

    @Autowired
    MovieAreaService movieAreaService;

    /**
     * 电影详情
     * @param id
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/detail/{id}")
    public ModelAndView toPageMovieDetail(@PathVariable("id") Long id) throws ExecutionException, InterruptedException {
        MovieDetailVo movieDetailVo = movieService.queryMovieDetail(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("movieDetailVo",movieDetailVo);
        modelAndView.setViewName("movieDetail");
        return modelAndView;
    }

    /**
     * 想看
     * @return
     */
    @RequestMapping("/wantSee/{id}")
    public R wantSee(@PathVariable("id") Long id){
        R r = movieService.wantSee(id);
        return r;
    }

    /**
     * 电影页
     * @param movieShow 电影显示状态 1正在热映 2即将上映 3经典影片
     * @param movieType 电影类型
     * @param movieArea 电影区域
     * @param movieSort 电影排序 1热门排序 2时间排序 3评论排序
     * @return
     */
    @RequestMapping("/movies")
    public ModelAndView movies(@RequestParam(defaultValue = "1") String movieShow,
                               @RequestParam(required = false) String movieType,
                               @RequestParam(required = false) String movieArea,
                               @RequestParam(defaultValue = "1") String movieSort,
                               @RequestParam(defaultValue = "1") Long pageNum
    ){
        ModelAndView modelAndView=new ModelAndView();
        //得到电影类型
        List<MovieType> typeList= movieTypeService.getMovieTypes();
        modelAndView.addObject("movieTypes",typeList);
        //得到电影区域
        List<MovieArea> areaLis= movieAreaService.getMovieAreas();
        modelAndView.addObject("movieAreas",areaLis);
        //得到电影数据
        IPage<Movie> page =movieService.conditionQueryMovie(movieShow,movieType,movieArea,movieSort,pageNum);
        modelAndView.addObject("page",page);
        modelAndView.setViewName("movies");
        return modelAndView;
    }


}

